Skip to content

Conversation

@marla-hoggard
Copy link
Contributor

@marla-hoggard marla-hoggard commented Apr 16, 2025

Overview

Writes unit tests for each of the api token service methods.

Since these methods call each other, I ran into to issues with proper mocking when they were all in one file, so I reorganized to put each method in its own file, and created a test file for each.

Generally speaking, each method was just copy/pasted into the new file, but I did make a couple updates:

  • Removed all the parameters from initAPIToken - they were only added for the purpose of testing and it turns out I didn't need them
  • I moved the validate method we pass into prompt to a named function so it could be tested

Context

https://linear.app/dittowords/issue/DIT-10076/add-tests-for-apitoken-service

Test Plan

Testing successfully completed in via:

  • The tests should pass in CI
  • The tests should pass locally by running npx jest apiToken (to run all the tests in the new directory)
  • How do they look?

],
watchPathIgnorePatterns: ["<rootDir>/.testing/", "<rootDir>/testing/"],
collectCoverageFrom: ["lib/**/*.{js,jsx,ts,tsx}"],
restoreMocks: true,
Copy link
Contributor Author

@marla-hoggard marla-hoggard Apr 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, I still have calls in each test file for this, but you can set this here to automatically restore. Seemed like a nice catch-all in case we forget somewhere.

https://jestjs.io/docs/configuration#restoremocks-boolean

priorToken = appContext.apiToken;
priorHost = appContext.apiHost;
appContext.setApiToken("");
appContext.apiHost = apiHost;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the functions that use appContext values that we want to specify, i'm saving the original value, changing it to what we want, then restoring the prior value after the test. This might be total overkill, but it seemed like a nice safety measure. Thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine for now.

expect(logger.url).toHaveBeenCalled();
expect(logger.bold).toHaveBeenCalled();
expect(logger.info).toHaveBeenCalled();
expect(logger.writeLine).toHaveBeenCalled();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this overkill? Should i ditch the logger checks? Does it make the test too brittle/resistent to change?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's okay to be brittle here as this will rarely change, and it does help us know that the messages were logged out. if the messages weren't getting logged out, that would be useful to know.

import collectAndSaveToken from "./collectAndSaveToken";
import validateToken from "./validateToken";
import getURLHostname from "./getURLHostname";

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method was updated to remove the optional parameters (never used in the real code) and hard-code to the appContext values. Would be a good idea to sanity check me here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's fine to remove the optional parameters, it should all still work the same.

return result.output?.join("\n") || "Invalid API key";
}
return true;
};
Copy link
Contributor Author

@marla-hoggard marla-hoggard Apr 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved the validate fn to its own saved function so it could be tested. It's identical to what was previously defined inline on line 22.

Copy link
Contributor

@jaerod95 jaerod95 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested the branching logic for the api token service and everything seems to be in working order!

@marla-hoggard marla-hoggard merged commit ca288e1 into v5-beta Apr 17, 2025
1 check passed
@jaerod95 jaerod95 deleted the marla/dit-10076-token-tests branch April 22, 2025 16:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants